const Merge = require('webpack-merge');
const webpack = require('webpack');
const CommonConfig = require('./webpack.config.js');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const publicPath = '/';
const ver = new Date().getTime() ;
module.exports = Merge(CommonConfig, {
    // entry: {

    optimization: {
        runtimeChunk: {
            name: 'manifest'
        },
        // minimizer: true, 
        splitChunks: {
            chunks: 'async',
            minSize: 30000,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            name: false,
            cacheGroups: {
                vendor: {
                    name: 'vendor',
                    chunks: 'initial',
                    priority: -10,
                    reuseExistingChunk: false,
                    test: /node_modules\/(.*)\.js/
                },
                styles: {
                    name: 'styles',
                    test: /\.(scss|css)$/,
                    chunks: 'all',
                    minChunks: 1,
                    reuseExistingChunk: true,
                    enforce: true
                }
            }
        }
    },
    output: {
        //测试环境使用BrowserRouter 根目录
        publicPath,
        path: __dirname + "/build", //打包后的文件存放的地方
        filename: "js/[name].[hash:6].js", //打包后输出文件的文件名
        chunkFilename: "js/[name].[chunkhash:6].js",
    },
    plugins: [

        new HtmlWebPackPlugin({
            template: './app/index.tmpl.html',
            filename: './index.html',
            publicPath,
            ver
        }),
        //减少moment中的语言包
        new webpack.ContextReplacementPlugin(
            /moment[\\\/]locale$/,
            /^\.\/(zh-cn)$/
        ),
        //打包时候清空文件夹
        new CleanWebpackPlugin(
            [
                'build',
            ], {
                //根目录
                root: __dirname,
                //开启在控制台输出信息   　　　　　　　　　　
                verbose: true,
                //启用删除文件        　　　　　　　　　　
                dry: false
            }
        ),
        //拷贝指定文件文件到编译目录
        new CopyWebpackPlugin([{
            from: __dirname + '/app/public/dist',
            to: __dirname + '/build/dist'
        }]),
        //不显示错误
        new webpack.NoEmitOnErrorsPlugin(),
        //性能监控
        // new BundleAnalyzerPlugin()
    ]
})